#!/bin/bash

# 创建 macOS .app 应用程序包的脚本

APP_NAME="FreeAugmentCode"
APP_DIR="${APP_NAME}.app"

echo "正在创建 macOS 应用程序包..."

# 创建应用程序目录结构
mkdir -p "${APP_DIR}/Contents/MacOS"
mkdir -p "${APP_DIR}/Contents/Resources"

# 创建 Info.plist 文件
cat > "${APP_DIR}/Contents/Info.plist" << EOF
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>CFBundleExecutable</key>
    <string>FreeAugmentCode</string>
    <key>CFBundleIdentifier</key>
    <string>com.freeaugmentcode.app</string>
    <key>CFBundleName</key>
    <string>Free AugmentCode</string>
    <key>CFBundleVersion</key>
    <string>1.0</string>
    <key>CFBundleShortVersionString</key>
    <string>1.0</string>
    <key>CFBundlePackageType</key>
    <string>APPL</string>
    <key>LSMinimumSystemVersion</key>
    <string>10.14</string>
    <key>NSHighResolutionCapable</key>
    <true/>
</dict>
</plist>
EOF

# 创建启动脚本
cat > "${APP_DIR}/Contents/MacOS/FreeAugmentCode" << 'EOF'
#!/bin/bash

# 获取应用程序包的路径
APP_PATH="$(dirname "$0")/../Resources"
cd "$APP_PATH"

# 检查 Python
if command -v python3 &> /dev/null; then
    PYTHON_CMD="python3"
elif command -v python &> /dev/null; then
    PYTHON_CMD="python"
else
    osascript -e 'display dialog "未找到 Python，请先安装 Python 3.8 或更高版本" buttons {"确定"} default button "确定"'
    exit 1
fi

# 在终端中运行程序
osascript << 'APPLESCRIPT'
tell application "Terminal"
    activate
    do script "cd '" & (system attribute "APP_PATH") & "' && python3 FreeAugmentCode-macOS.py"
end tell
APPLESCRIPT
EOF

# 设置执行权限
chmod +x "${APP_DIR}/Contents/MacOS/FreeAugmentCode"

# 复制程序文件到 Resources 目录
cp index.py "${APP_DIR}/Contents/Resources/"
cp FreeAugmentCode-macOS.py "${APP_DIR}/Contents/Resources/"
cp -r augutils "${APP_DIR}/Contents/Resources/"
cp -r utils "${APP_DIR}/Contents/Resources/"

echo "✓ macOS 应用程序包创建完成: ${APP_DIR}"
echo ""
echo "使用方法:"
echo "1. 双击 ${APP_DIR} 运行应用程序"
echo "2. 或者在终端中运行: open ${APP_DIR}"
echo ""
echo "注意: 首次运行可能需要在系统偏好设置中允许运行"
